package net.lastcoder.action;

import net.lastcoder.ifserver.game.Client;
import net.lastcoder.ifserver.game.GameManager;
import net.lastcoder.ifserver.plugin.Plugin;
import net.lastcoder.ifserver.plugin.PluginManager;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.function.BiConsumer;

/**
 * 一个简单的聊天服务端，
 * 热更新可以通过tools中的例子快速更新
 * auth: WenYF
 * date: 2017/2/6
 */
public class ActionPlugin implements Plugin {
    private static final Logger Log = LoggerFactory.getLogger(ActionPlugin.class);

    private BiConsumer<Client, String> handler = (client, str) -> {
        try {
            client.send(str);
        } catch (IOException e) {
            Log.error(e.getLocalizedMessage(), e);
        }
    };

    @Override
    public void initializePlugin(PluginManager manager, File pluginDirectory) {
        GameManager.getInstance().putActionHandler("action", handler);
        Log.debug("init action plugin success.");
    }

    @Override
    public void destroyPlugin() {
        GameManager.getInstance().removeActionHandler("action");
        Log.debug("deinit action plugin success.");
    }

}
